﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Edc.Tfs2MantisGui
{
    public partial class FormIssue : Form
    {
        public FormIssue()
        {
            InitializeComponent();
        }

        public Edc.MantisClient.Mantis Mantis { get; set; }

        public Edc.MantisClient.Issue Issue { get; set; }

        private void txtIssueId_Validating(object sender, CancelEventArgs e)
        {
            this.Issue = null;

            if (txtIssueId.Text == "")
            {
                this.txtIssueSummary.Text = "";
                return;
            }

            int issueId = 0;
            try
            {
                issueId = Int32.Parse(txtIssueId.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                e.Cancel = true;
                return;
            }

            try
            {
                var issue = Mantis.GetIssue(issueId);

                this.Issue = issue;
                this.txtIssueSummary.Text = issue.Summary;
            }
            catch (Edc.MantisClient.MantisException ex)
            {
                MessageBox.Show(ex.Message, "Errore", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                e.Cancel = true;
            }
        }

        private void txtIssueId_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != '\r') return;
            txtIssueSummary.Select();
            txtIssueId.Select();
        }

        private void FormAssembly_Load(object sender, EventArgs e)
        {
            if (this.Issue != null) this.txtIssueId.Text = this.Issue.Id.ToString();
            this.Mantis.Working += new EventHandler(Mantis_Working);
            this.Mantis.Done += new EventHandler(Mantis_Done);
        }

        void Mantis_Working(object sender, EventArgs e)
        {
            this.Enabled = false;
        }

        void Mantis_Done(object sender, EventArgs e)
        {
            this.Enabled = true;
        }
    }
}
